package util;

import com.obs.services.ObsClient;
import com.obs.services.model.ObsObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * @program: sijiacheng-obs-use
 * @create: 2023-06-29
 * 下载对象
 **/
public class OBSDownloadFile {
    public static void main(String[] args) throws Exception {
        // Endpoint以实际情况填写。
        String endPoint = "https://obs.cn-south-1.myhuaweicloud.com";
        String ak = "RKN67AS*****LX55K";
        String sk = "ycddw9tac*****nC8cVLP2aANLmtQermn";

        // 创建ObsClient实例
        final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

        ObsObject obsObject = obsClient.getObject("obs-ggj", "mytest1");
        // 读取对象内容
        System.out.println("Object content:");
        InputStream input = obsObject.getObjectContent();
        byte[] b = new byte[1024];
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int len;
        while ((len=input.read(b)) != -1){
            bos.write(b, 0, len);
        }

        System.out.println(new String(bos.toByteArray(),"UTF-8"));
        bos.close();
        input.close();

    }
}
